home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Frameworks / MADE 1.0.1 / Essential Memory.c < prev    next >
Encoding:
Text File  |  1997-06-04  |  3.3 KB  |  157 lines  |  [TEXT/CWIE]

  1. // MADE - Macintosh Application Development Essentials
  2. // ---------------------------------------------------
  3.  
  4. // (c) Gideon Greenspan, Sig Software - June 1997 - http://www.kagi.com/gdg/
  5.  
  6. // These files can only be used for experimental standalone purposes. To obtain
  7. // fully commented code, and licenses for standalone, shareware, internal and
  8. // commercial usage, run the enclosed Register application.
  9.  
  10. // Essential Memory.c
  11. //
  12. // Memory allocation, deallocation and initialisation.
  13. //
  14. // Version 1.0.0 - 10th November 1996
  15. // Version 1.0.1 - 4th June 1997
  16. //                 Fixed macro, function names
  17.  
  18. #include "Essential Headers.h"
  19. #include "Essential Prototypes.h"
  20.  
  21. Error CreateHeapSpace(Size required)
  22. {
  23.     Error    error=0;
  24.     Size    allocate;
  25.     
  26.     allocate=required+Emergency_Memory_Reserve;
  27.     
  28.     if (MaxBlock()<allocate) {
  29.     
  30.         PurgeMem(allocate);
  31.         if (MaxBlock()<allocate) {
  32.         
  33.             MyFreeUpMemory(allocate);
  34.             
  35.             if (MaxBlock()<allocate)
  36.                 error=memFullErr;
  37.         }
  38.     }
  39.     
  40.     TestError(error);
  41.     return error;
  42. }
  43.  
  44. void*    AllocPtr(Error* error, Size allocate)
  45. {
  46.     void*    pointer;
  47.     
  48.     *error=CreateHeapSpace(allocate);
  49.         _i(*error)
  50.         
  51.     pointer=(void*)NewPtr(allocate);
  52.     *error=TestMemError(pointer);
  53.         _i(*error);
  54.         
  55.     #if Project_Under_Development && Initialise_Allocated_Memory
  56.         InitialiseMemory(pointer, allocate);
  57.     #endif    
  58.  
  59.     return pointer;
  60.     _e
  61.     return 0;
  62. }
  63.  
  64. void**    AllocHandle(Error* error, Size allocate)
  65. {
  66.     void**    handle;
  67.     
  68.     *error=CreateHeapSpace(allocate);
  69.         _i(*error)
  70.         
  71.     handle=(void**)NewHandle(allocate);
  72.     *error=TestMemError(handle);
  73.         _i(*error);
  74.         
  75.     #if Project_Under_Development
  76.         #if Move_On_Handle_Allocation
  77.             MoveHHi((Handle)handle);
  78.         #endif
  79.  
  80.         #if Initialise_Allocated_Memory
  81.             InitialiseMemory(*handle, allocate);
  82.         #endif
  83.     #endif    
  84.  
  85.     return handle;
  86.     _e
  87.     return 0;
  88. }
  89.  
  90. void    DestroyPtr(void* pointer)
  91. {
  92.     #if Project_Under_Development && Initialise_Allocated_Memory
  93.         InitialiseMemory(pointer, GetPtrSize((Ptr)pointer));
  94.     #endif    
  95.     DisposPtr((Ptr)pointer);
  96. }
  97.  
  98. void    DestroyHandle(void** handle)
  99. {
  100.     #if Project_Under_Development && Initialise_Allocated_Memory
  101.         InitialiseMemory(*handle, GetHandleSize((Handle)handle));
  102.     #endif    
  103.     DisposHandle((Handle)handle);
  104. }
  105.  
  106. #if Project_Under_Development
  107.  
  108.     #if Assert_Memory_Locking
  109.     
  110.         // I'm sure the next two routines could be written better with more knowledge of
  111.         // Apple's System Software, but the programming guidelines don't offer such
  112.         // information, so it's likely to change. Thus I do this the long way round.
  113.         
  114.         void    LockHandleAssert(void** handle)
  115.         {
  116.             SignedByte    statusBefore, statusAfter;
  117.             
  118.             statusBefore=HGetState((Handle)handle);
  119.             HLock((Handle)handle);
  120.             statusAfter=HGetState((Handle)handle);
  121.             
  122.             Assert(statusBefore!=statusAfter);
  123.         }
  124.         
  125.         void    UnlockHandleAssert(void** handle)
  126.         {
  127.             SignedByte    statusBefore, statusAfter;
  128.             
  129.             statusBefore=HGetState((Handle)handle);
  130.             HUnlock((Handle)handle);
  131.             statusAfter=HGetState((Handle)handle);
  132.             
  133.             Assert(statusBefore!=statusAfter);
  134.         }
  135.         
  136.     #endif
  137.     
  138.     #if Initialise_Allocated_Memory
  139.  
  140.         // This could be optimised by first filling with long words of 0xAAAAAAAA, but there's
  141.         // little point as it's only used when the project is under development anyway.
  142.         
  143.         void    InitialiseMemory(char* pointer, Size length)
  144.         {
  145.             char    *trasher, *topAddress;
  146.             
  147.             trasher=pointer;
  148.             topAddress=trasher+length;
  149.             
  150.             while (trasher<topAddress)
  151.                 *trasher++=0xAA;
  152.         }
  153.         
  154.     #endif
  155.  
  156. #endif
  157.